﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.Client.Common.Session;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    public class OrganizationMembersClassesManager
    {

        private static msgOrganizationTreeMembersClass[] _OrganizationMembersClasses;

        public OrganizationMembersClassesManager()
        {

        }

        public static msgOrganizationTreeMembersClass[] MembersClasses
        {
            get 
            {
                return _OrganizationMembersClasses;
            }
    
        }

        public static msgOrganizationTreeMembersClass GetOrganizationTreeMemberClass(int MemberID)
        { 
            int i;
            for (i = 0; i < _OrganizationMembersClasses.Length; i++)
            {
                if (_OrganizationMembersClasses[i].OrganizationMemberClassID == MemberID)
                    return _OrganizationMembersClasses[i];
            }
            return null;
        }

        public static int GetOrganizationMemberClassIDByName(string ClassName)
        {
            int i;
            for (i = 0; i < _OrganizationMembersClasses.Length; i++)
            {
                if (_OrganizationMembersClasses[i].Description  == ClassName)
                    return _OrganizationMembersClasses[i].OrganizationMemberClassID;
            }
            return 0;
        }

        public static iOrganizationTreeMemberClass GetOrganizationTreeMemberEditor(msgOrganizationMemberInfo inf)
        {

            foreach (msgOrganizationTreeMembersClass x in _OrganizationMembersClasses)
            {
                if (x.OrganizationMemberClassID == inf.MemberClass) 
                {
                    iOrganizationTreeMemberClass m = (iOrganizationTreeMemberClass)Activator.CreateInstance(System.Type.GetType(x.AssemblyAndClass));
                    m.InitClass(x);
                    m.LoadMemberData(inf);
                    return m;
                }

            }
            return null;
        }

        public static iOrganizationTreeMemberClass GetOrganizationTreeMemberEditor(int NodeID, string Description, int MemberClass)
        {
            string extendedProperties = GetOrganizationTreeMemberClass(MemberClass).BasicProperties.DocumentXML;  
            msgNewOrganizationMember req = new msgNewOrganizationMember(NodeID, Description, MemberClass, extendedProperties, 0, "", "", OrganizationChart.MembersClasses.MemberBase.DefaultLanguage(), 0, new int[0]);
            msgNewOrganizationTreeItemResponse rsp = BusinessActions.CreateNewOrganizationMember(req);
            return GetOrganizationTreeMemberEditor(new msgOrganizationMemberInfo(rsp.NodeID, Description, NodeID, MemberClass, extendedProperties, 0, "", "", OrganizationChart.MembersClasses.MemberBase.DefaultLanguage(), new msgMembershipInfo(0, 0, ""), new msgMembershipInfo[0], rsp.UpdateSequence));
        }

        public static void LoadOrganizationTreeMembersClasses()
        {
            _OrganizationMembersClasses  =  BusinessActions.GetOrganizationTreeMembersClasses();
        }

        public static iOrganizationTreeMemberClass[] OrganizationTreeMemberClasses
        {
            get {
                iOrganizationTreeMemberClass[] rsp = new iOrganizationTreeMemberClass[_OrganizationMembersClasses.Length];
                int i = 0;
                foreach (msgOrganizationTreeMembersClass x in _OrganizationMembersClasses)
                {
                   iOrganizationTreeMemberClass m = (iOrganizationTreeMemberClass)Activator.CreateInstance(System.Type.GetType(x.AssemblyAndClass));
                   m.InitClass(x);
                   rsp[i] = m;
                   i++;
                }
                return rsp;                       
            }
        }
    }
}
